package com.starlinkdt.common.enums;

import com.starlinkdt.common.utils.mask.DataMaskingOperation;
import org.springframework.util.StringUtils;

/**
 * @Author cuixuan
 * @Description 脱敏转换操作枚举
 * @Date 2025/6/11 20:41
 */
public enum DataMaskingFunc {

    /**
     * 脱敏转换器
     */
    NO_MASK((str, maskChar) -> str),
    /**
     * 身份证/驾驶证
     */
    ID_MASK((str, maskChar) -> {
        if (StringUtils.hasLength(str)) {
            StringBuilder sb = new StringBuilder();
            String pre = str.substring(0, 6);
            String stuff = str.substring(str.length() - 4);
            sb.append(pre);
            sb.append((StringUtils.hasLength(maskChar) ? maskChar : "*").repeat(Math.max(0, str.length() - 10)));
            sb.append(stuff);
            return sb.toString();
        } else {
            return str;
        }
    }),
    /**
     * 手机号
     */
    PHONE_MASK((str, maskChar) -> {
        if (StringUtils.hasLength(str)) {
            StringBuilder sb = new StringBuilder();
            String pre = str.substring(0, 3);
            String stuff = str.substring(str.length() - 4);
            sb.append(pre);
            sb.append((StringUtils.hasLength(maskChar) ? maskChar : "*").repeat(Math.max(0, str.length() - 7)));
            sb.append(stuff);
            return sb.toString();
        } else {
            return str;
        }
    }),
    /**
     * 全码
     */
    ALL_MASK((str, maskChar) -> {
        if (StringUtils.hasLength(str)) {
            return (StringUtils.hasLength(maskChar) ? maskChar : DataMaskingOperation.MASK_CHAR).repeat(str.length());
        } else {
            return str;
        }
    });

    private final DataMaskingOperation operation;

    DataMaskingFunc(DataMaskingOperation operation) {
        this.operation = operation;
    }

    public DataMaskingOperation operation() {
        return this.operation;
    }
}
